package com.fyzk.activity.lottery.togetherbuy;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.app.ActivityTaskManager;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.popupwindow.KeyBoardPopupwindow;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.bean.FollowPerson;
import com.fyzk.bean.JCOrderInfo;
import com.fyzk.bean.NumOrderInfo;
import com.fyzk.bean.TogetherbuyOrderDetail;
import com.fyzk.bean.ZCOrderInfo;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.dialog.DJ_ProgressDialog;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.fyzk.view.ViewCallBack;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.DonutProgress;
import com.fyzk.view.NoScrollListView;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import http.Common.CommandUtil;
import http.DataUtil.text.TextUtil;
import http.StringResultBean;

@ContentView(R.layout.hm_gendan)
public class HeMaiGenDanActivity extends BaseActivity implements onTopTitleClickListener {

    @ViewInject(R.id.hm_gendan_topTitle)
    private TopTitleView topView;
    @ViewInject(R.id.hm_gendan_lottery_tv)
    private TextView lottery_tv;
    @ViewInject(R.id.join_together)
    private Button join_together;
    @ViewInject(R.id.amount_pay_txt)
    private TextView amount_pay_txt;
    @ViewInject(R.id.hm_gendan_content_wincode)
    private TextView hm_gendan_content_wincode;
    @ViewInject(R.id.amount_pay)
    private TextView amount_pay;
    @ViewInject(R.id.hm_gendan_content_userfaqi)
    private TextView userfaqi;
    @ViewInject(R.id.hm_gendan_content_percentage)
    private TextView percentage;
    @ViewInject(R.id.hm_gendan_content_state)
    private TextView content_state;
    @ViewInject(R.id.bottom_view)
    private FrameLayout bottom_view;

    @ViewInject(R.id.balance)
    private TextView balance;

    @ViewInject(R.id.hm_leftmoney_tv)
    private TextView hm_leftmoney_tv;

    @ViewInject(R.id.hm_gendan_content_msg)
    private TextView content_msg;

    @ViewInject(R.id.hm_money_tv)
    private TextView hm_money_tv;

    @ViewInject(R.id.gendan_status)
    private TextView gendan_status;

    @ViewInject(R.id.hm_persons_tv)
    private TextView hm_persons_tv;

    @ViewInject(R.id.hm_list_safe_tv)
    private TextView hm_list_safe_tv;

    @ViewInject(R.id.open_state)
    private TextView open_state;

    @ViewInject(R.id.order_id)
    private TextView order_id;

    @ViewInject(R.id.persons)
    private NoScrollListView personsList;

    @ViewInject(R.id.create_time)
    private TextView create_time;

    @ViewInject(R.id.end_time)
    private TextView end_time;
    @ViewInject(R.id.hm_gendan_content_garantee)
    private TextView content_garantee;
    @ViewInject(R.id.hm_gendan_content_buypersonnum)
    private TextView content_buypersonnum;

    @ViewInject(R.id.hm_item_progress)
    private DonutProgress hm_item_progress;

    @ViewInject(R.id.fg_ordercontent)
    private FrameLayout fg_ordercontent;

    @ViewInject(R.id.fg_persons)
    private FrameLayout fg_persons;
    @ViewInject(R.id.persons_ll)
    private LinearLayout persons_ll;


    @ViewInject(R.id.fg_content_msg)
    private FrameLayout fg_msg;

    @ViewInject(R.id.fg_gendan_mingxi)
    private FrameLayout fg_gendan_mingxi;

    @ViewInject(R.id.ll_content_msg)
    private LinearLayout ll_content_msg;

    @ViewInject(R.id.hm_ordercontent_iv)
    private ImageView ordercontent_jietou;

    @ViewInject(R.id.hm_ordermsg_iv)
    private ImageView ordermsg_jietou;

    @ViewInject(R.id.hm_orderpersons_iv)
    private ImageView orderpersons_jietou;

    @ViewInject(R.id.hm_gendan_content_expectedprize_tv)
    private TextView expectedPrize_tv;

    @ViewInject(R.id.create_title)
    private TextView hm_title_tv;
    @ViewInject(R.id.create_content)
    private TextView hm_content_tv;

    @ViewInject(R.id.persons_more_tv)
    private TextView persons_more_tv;

    //数据
    private String id;
    private String userName = "";
    private int buyAmount = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        id = getIntent().getStringExtra(IntentParamUtils.HM_HALLDETAIL_ID);
        initView();
        requestHMDetail();
        balance.setText("账户彩金：" + MySharedPreferences.getInstance().getBalance() + "米");
//        amount_pay.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                showPayAmountDialog();
//                return true;
//            }
//        });
        amount_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPayAmountDialog();
            }
        });
    }

    private void requestHMPersonDetail() {
        try {
            JSONObject request_json = new JSONObject();
            request_json.put("caseId", id);
            request_json.put("pageIndex", 1);
            request_json.put("maxResult", 50);
            httpPostJson(CommandUtil.FINDCASELOTBUYDETAILS, Commands.FINDCASELOTBUYDETAILS,
                    request_json, true);
        } catch (JSONException e) {

        }
    }

    private void requestHMDetail() {
        try {
            JSONObject request_json = new JSONObject();
            request_json.put("caseId", id);
            httpPostJson(CommandUtil.CASELOTDETAIL, Commands.CASELOTDETAIL, request_json, true);
        } catch (JSONException e) {

        }
    }

    @Event(R.id.join_together)
    private void joinTogether(View view) {
        if (!MySharedPreferences.getInstance().getLoginStatus()) {
            AppLotteryInfos.clear();
            ViewCallBack.getInstance().countDownObjs.clear();
            ActivityTaskManager.getInstance().closeAllActivity();
            Intent intent = new Intent(HeMaiGenDanActivity.this, LoginActivity.class);
            startActivity(intent);
        } else {
            try {
                JSONObject request_json = new JSONObject();
                request_json.put("buyAmount", buyAmount * 100);
                request_json.put("safeAmount", 0);
                request_json.put("caseId", id);
                httpPostJson(CommandUtil.BETCASELOT, Commands.BETCASELOT, request_json, true);
            } catch (JSONException e) {

            }
        }

    }

    private int max;
    private int min = 1;

    private KeyBoardPopupwindow keyBoardPopupwindow;

    private void showPayAmountDialog() {
        if (keyBoardPopupwindow == null) {
            keyBoardPopupwindow = new KeyBoardPopupwindow(getContext(), max, "买", "份");
            keyBoardPopupwindow.setMinMultiple(min);
            keyBoardPopupwindow.hideTop_ll();
            keyBoardPopupwindow.setOnKeyBoardListener(new KeyBoardPopupwindow.OnKeyBoardListener() {
                @Override
                public void onItem(int position) {

                }

                @Override
                public void popupShow() {

                }

                @Override
                public void popupDismiss(int multiple) {
                    amount_pay.setText("" + multiple);
                    buyAmount = multiple;
                    String all_moneystr = "共<font color=\"#f8dd26\">" + buyAmount +
                            "</font>米";
                    amount_pay_txt.setText(Html.fromHtml(all_moneystr));
                }
            });
        }
        keyBoardPopupwindow.showPopupWindow(amount_pay, amount_pay.getText() + "");

    }

    TogetherbuyOrderDetail togetherbuyOrderDetail;
    View personHeader = null;
    private List<FollowPerson> persons = new ArrayList<>();
    PersonsAdapter personsAdapter = null;
    List<FollowPerson> personsInfors = new ArrayList<>();
    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.BETCASELOT:
                if (resultBean.getErrorcode().equals("0")) {
                    showToast("参与合买成功");
                    requestHMDetail();
                    requestHMPersonDetail();
                }
                break;
            case CommandUtil.CASELOTDETAIL:
                if (resultBean.getErrorcode().equals("0")) {
                    togetherbuyOrderDetail = JSON.parseObject(resultBean.getData(),
                            TogetherbuyOrderDetail.class);
                    setViewData();
                    requestHMPersonDetail();
                } else {
                    showToast(resultBean.getMessage());
                }
                break;
            case CommandUtil.FINDCASELOTBUYDETAILS:
                if (resultBean.getErrorcode().equals("0")) {
                    try {
                        JSONObject jsonObject = new JSONObject(resultBean.getData());
                        List<FollowPerson> persons_net = JSON.parseArray(jsonObject.getString
                                ("caselotbuys"), FollowPerson.class);
                        personsInfors = persons_net;
                        if (persons_net.size() > 10) {
                            persons_more_tv.setVisibility(View.VISIBLE);
                            persons_net = persons_net.subList(0,10);
                        } else {
                            persons_more_tv.setVisibility(View.GONE);
                        }
                        personsList.setVisibility(View.VISIBLE);
                        if (personHeader == null) {
                            personHeader = View.inflate(HeMaiGenDanActivity.this, R.layout
                                    .item_togetherbuy_followperson_top, null);
                            personsList.addHeaderView(personHeader);
                            persons.addAll(persons_net);
                            personsAdapter = new PersonsAdapter(persons);
                            personsList.setAdapter(personsAdapter);
                        } else {
                            persons.clear();
                            persons.addAll(persons_net);
                            personsAdapter.notifyDataSetChanged();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
                break;
        }
    }

    class PersonsAdapter extends BaseAdapter {

        List<FollowPerson> persons;

        PersonsAdapter(List<FollowPerson> persons) {
            this.persons = persons;
        }

        @Override
        public int getCount() {
            return persons.size();
        }

        @Override
        public FollowPerson getItem(int position) {
            return persons.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (holder == null) {
                holder = new ViewHolder();
                convertView = View.inflate(HeMaiGenDanActivity.this, R.layout
                        .item_togetherbuy_followperson, null);
                holder.fenNum = convertView.findViewById(R.id.followperson_buyAmount);
                holder.nickName = convertView.findViewById(R.id.followperson_nickname);
                holder.prize = convertView.findViewById(R.id.followperson_prize);
                holder.time = convertView.findViewById(R.id.followperson_time);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            FollowPerson person = getItem(position);
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
            holder.time.setText(dateFormat.format(new Date(person.getBuyTime())));
            if (togetherbuyOrderDetail != null && togetherbuyOrderDetail.getDisplayState() == 6) {
                holder.prize.setText(PublicMethod.formatMoney(person.getPrizeAmt() * 1f));
            } else {
                holder.prize.setText("--");
            }
            holder.nickName.setText(PublicMethod.getSenderName(person.getAlias(), person.getUsername()));
            holder.fenNum.setText("" + PublicMethod.formatMoney(person.getBuyAmount()));
            return convertView;
        }

        class ViewHolder {
            TextView nickName;
            TextView fenNum;
            TextView time;
            TextView prize;
        }
    }


    private final int ALLOPEN = 5;
    private final int ENDOPEN = 2;
    private final int FOLLOWOPEN = 3;
    private final int NEVEROPEN = 1;

    private void setViewData() {
        lottery_tv.setText(togetherbuyOrderDetail.getLotteryTypeName() + "-" +
                togetherbuyOrderDetail.getPhase() + "期");
        long startTime = togetherbuyOrderDetail.getStartTime();
        long endTime = togetherbuyOrderDetail.getEndTime();
        SimpleDateFormat dataFormat_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        create_time.setText("发起时间：" + dataFormat_ss.format(new Date(startTime)));
        end_time.setText("截至时间：" + dataFormat.format(new Date(endTime)));
        if (TextUtil.isEmpty(togetherbuyOrderDetail.getTitle())) {
            hm_title_tv.setVisibility(View.GONE);
        }
        hm_title_tv.setText("方案标题："+togetherbuyOrderDetail.getTitle());
        if (TextUtil.isEmpty(togetherbuyOrderDetail.getDescription())) {
            hm_content_tv.setVisibility(View.GONE);
        }
        hm_content_tv.setText("方案描述："+togetherbuyOrderDetail.getDescription());
        hm_item_progress.setProgress(togetherbuyOrderDetail.getBuyPercent());
        hm_item_progress.setText(togetherbuyOrderDetail.getBuyPercent() + "%");
        hm_list_safe_tv.setText("保底" + togetherbuyOrderDetail.getSafePercent() + "%");
        hm_money_tv.setText(PublicMethod.formatMoney(togetherbuyOrderDetail.getTotalAmt()) + "米");
        hm_persons_tv.setText(togetherbuyOrderDetail.getParticipantCount() + "人");
        hm_leftmoney_tv.setText(togetherbuyOrderDetail.getLeftNum() + "米");
        max = PublicMethod.formatMoney(togetherbuyOrderDetail.getTotalAmt() -
                togetherbuyOrderDetail.getBuyAmount());
        if (keyBoardPopupwindow != null) {
            keyBoardPopupwindow.setMaxMultiple(max);
        }
        userfaqi.setText("发起人：" + PublicMethod.getSenderName(togetherbuyOrderDetail.getAlias(), togetherbuyOrderDetail.getUsername()));
        int visible = togetherbuyOrderDetail.getVisibility();
        int commisionRatio = togetherbuyOrderDetail.getCommisionRatio();
        switch (visible) {
            case ALLOPEN:
                open_state.setText("完全公开");
                break;
            case ENDOPEN:
                open_state.setText("截止后公开");
                break;
            case FOLLOWOPEN:
                open_state.setText("跟单可见");
                break;
            case NEVEROPEN:
                open_state.setText("完全保密");
                break;
        }
        order_id.setText("订单号码：" + id);
        if (commisionRatio != 0) {
//            "共<font color=\"#f8dd26\">"+buyAmount+"</font>米";
            StringBuffer commisionStr = new StringBuffer();
            commisionStr.append("方案提成：提成税后奖金的");
            commisionStr.append(commisionRatio);
            commisionStr.append("%");
            if (togetherbuyOrderDetail.getCommisionPrizeAmt() != 0) {
                commisionStr.append(",本单发起人提成<font color=\"#ef3338\">");
                commisionStr.append(PublicMethod.formatMoney(togetherbuyOrderDetail
                        .getCommisionPrizeAmt() * 1f));
                commisionStr.append("米</font>");
            }
            percentage.setText(Html.fromHtml(commisionStr.toString()));
        } else {
            percentage.setText("方案提成：无提成");
        }
        StringBuffer displayStateStr = new StringBuffer();
        displayStateStr.append("方案状态：");
        content_msg.setText("方案彩金：" + togetherbuyOrderDetail.getBetNum() + "注，" +
                togetherbuyOrderDetail.getMutiple() + "倍, " + PublicMethod.formatMoney
                (togetherbuyOrderDetail.getTotalAmt()) + "米");
        content_garantee.setText("保底份额：" + togetherbuyOrderDetail.getSafeNum() + "份，占" +
                togetherbuyOrderDetail.getSafePercent() + "%");
        content_buypersonnum.setText("认购人次：" + togetherbuyOrderDetail.getParticipantCount() +
                "人，共" + PublicMethod.formatMoney(togetherbuyOrderDetail.getBuyAmount()) + "米");
        String info = togetherbuyOrderDetail.getOrderInfo();

        if (AppLotteryInfos.isJC(togetherbuyOrderDetail
                .getLotteryType())) {
            expectedPrize_tv.setText("预计奖金：若中奖，奖金约 " + togetherbuyOrderDetail.getExpectedPrize() + " 米");
            if (togetherbuyOrderDetail.getOrderResultStatus() == 4 || togetherbuyOrderDetail.getOrderResultStatus() == 2 ||
                    togetherbuyOrderDetail.getExpectedPrize() == null || togetherbuyOrderDetail.getExpectedPrize().equals("")) {
                expectedPrize_tv.setVisibility(View.GONE);
            } else {
                expectedPrize_tv.setVisibility(View.VISIBLE);
            }
        }
        NumOrderInfo orderInfo = null;
        ZCOrderInfo zcOrderInfo = null;
        JCOrderInfo jcOrderInfo = null;

        int displayState = togetherbuyOrderDetail.getDisplayState();
        boolean unable = false;
//        String displayStateStr = "认购中";



        fg_gendan_mingxi.removeAllViews();
        switch (displayState) {
            case 1://认购中
                unable = false;
                displayStateStr.append(togetherbuyOrderDetail.getDisplayStateMemo());
                break;
            case 2://已满员
            case 4://已撤单
            case 5://已流单
            case 7://出票失败
                unable = true;
                displayStateStr.append(togetherbuyOrderDetail.getDisplayStateMemo());
                break;
            case 8:// 未中奖
                unable = true;

                if (AppLotteryInfos.isNumLottery(togetherbuyOrderDetail.getLotteryType())) {
                    orderInfo = JSON.parseObject(info, NumOrderInfo.class);
                    hm_gendan_content_wincode.setVisibility(View.VISIBLE);
                    hm_gendan_content_wincode.setText("开奖号码：" + togetherbuyOrderDetail.getWincode());
                    displayStateStr.append(togetherbuyOrderDetail.getDisplayStateMemo());
                } else if (AppLotteryInfos.isOldFootballLottery(togetherbuyOrderDetail
                        .getLotteryType())) {
                    zcOrderInfo = JSON.parseObject(info, ZCOrderInfo.class);
                    displayStateStr.append(togetherbuyOrderDetail.getDisplayStateMemo());
                } else if (AppLotteryInfos.isJC(togetherbuyOrderDetail
                        .getLotteryType())) {
                    jcOrderInfo = JSON.parseObject(info, JCOrderInfo.class);
                    displayStateStr.append(togetherbuyOrderDetail.getDisplayStateMemo());
                }

                break;
            case 6://已中奖
                unable = true;
                if (AppLotteryInfos.isNumLottery(togetherbuyOrderDetail.getLotteryType())) {
                    orderInfo = JSON.parseObject(info, NumOrderInfo.class);
                    hm_gendan_content_wincode.setVisibility(View.VISIBLE);
                    hm_gendan_content_wincode.setText("开奖号码：" + togetherbuyOrderDetail.getWincode());
                    displayStateStr.append("<font color=\"");
                    displayStateStr.append(getResources().getColor(R.color.top_bg));
                    displayStateStr.append("\">");
                    displayStateStr.append(togetherbuyOrderDetail.getDisplayStateMemo());
                    displayStateStr.append(" 中奖彩金:");
                    displayStateStr.append(PublicMethod.formatMoney(togetherbuyOrderDetail.getPrize() * 1f));
                    displayStateStr.append("米");
                    displayStateStr.append("</font>");
                } else if (AppLotteryInfos.isOldFootballLottery(togetherbuyOrderDetail
                        .getLotteryType())) {
                    zcOrderInfo = JSON.parseObject(info, ZCOrderInfo.class);
                    displayStateStr.append("<font color=\"");
                    displayStateStr.append(getResources().getColor(R.color.top_bg));
                    displayStateStr.append("\">");
                    displayStateStr.append(togetherbuyOrderDetail.getDisplayStateMemo());
                    displayStateStr.append(" 中奖彩金:");
                    displayStateStr.append(PublicMethod.formatMoney(togetherbuyOrderDetail.getPrize() * 1f));
                    displayStateStr.append("米");
                    displayStateStr.append("</font>");
                } else if (AppLotteryInfos.isJC(togetherbuyOrderDetail
                        .getLotteryType())) {
                    jcOrderInfo = JSON.parseObject(info, JCOrderInfo.class);
                    displayStateStr.append("<font color=\"");
                    displayStateStr.append(getResources().getColor(R.color.top_bg));
                    displayStateStr.append("\">");
                    displayStateStr.append(togetherbuyOrderDetail.getDisplayStateMemo());
                    displayStateStr.append(" 中奖彩金:");
                    displayStateStr.append(PublicMethod.formatMoney(togetherbuyOrderDetail
                            .getPrize() * 1f));
                    displayStateStr.append("米");
                    displayStateStr.append("</font>");
                }

                break;
            default:
                unable = true;
                displayStateStr.append(togetherbuyOrderDetail.getDisplayStateMemo());
                break;
        }
        content_state.setText(Html.fromHtml(displayStateStr.toString()));
        if (info != null) {
            if (AppLotteryInfos.isNumLottery(togetherbuyOrderDetail.getLotteryType())) {
                orderInfo = JSON.parseObject(info, NumOrderInfo.class);
                fg_gendan_mingxi.addView(addContent(orderInfo));
            } else if (AppLotteryInfos.isOldFootballLottery(togetherbuyOrderDetail.getLotteryType
                    ())) {
                zcOrderInfo = JSON.parseObject(info, ZCOrderInfo.class);
                fg_gendan_mingxi.addView(addZCContent(zcOrderInfo));
            } else {
                jcOrderInfo = JSON.parseObject(info, JCOrderInfo.class);
                fg_gendan_mingxi.addView(addJCContent(jcOrderInfo));

            }
        }

        if (togetherbuyOrderDetail.getBuyType() == 1) {
            gendan_status.setText("我发起");
            gendan_status.setBackgroundResource(R.drawable.shape_ovalleft_rectright_green);
        } else {
            if (togetherbuyOrderDetail.getJoin() == 1) {
                gendan_status.setText("已参与");
                gendan_status.setBackgroundResource(R.drawable.shape_ovalleft_rectright_green);
            } else {
                gendan_status.setText("未参与");
                gendan_status.setBackgroundResource(R.drawable.shape_ovalleft_rectright_red);
            }
        }
        String balance_ = PublicMethod
                .formatBalance(togetherbuyOrderDetail.getBalance());
        MySharedPreferences.getInstance().setBalance(balance_);
        balance.setText("账户彩金：" + balance_ + "米");
        if (unable) {
            balance.setVisibility(View.GONE);
            bottom_view.setVisibility(View.GONE);
        }else{
            balance.setVisibility(View.VISIBLE);
            bottom_view.setVisibility(View.VISIBLE);
        }

    }


    protected View addContent(NumOrderInfo info) {
        LinearLayout layout_all = new LinearLayout(this);
        layout_all.setOrientation(LinearLayout.VERTICAL);
        for (NumOrderInfo.DetailBean bean : ((NumOrderInfo) info).getDetail()) {
            LinearLayout layout = new LinearLayout(this);
            layout.setOrientation(LinearLayout.VERTICAL);
            TextView wf = new TextView(this);
            wf.setTextColor(getResources().getColor(R.color.color_666666));
            wf.setText("玩法：" + bean.getPlayName());
            layout.addView(wf);
            TextView code = new TextView(this);
            code.setTextColor(getResources().getColor(R.color.color_666666));
            code.setText(bean.getBetcode());
            layout.addView(code);
            layout_all.addView(layout);
        }
        return layout_all;
    }


    protected View addZCContent(ZCOrderInfo info) {
        View view = View.inflate(this, R.layout.layout_zcbetcode, null);
        TextView playName = (TextView) view.findViewById(R.id.playName);
        playName.setText("玩法:" + info.getDetail().getPlayName());
        ListView ls = (ListView) view.findViewById(R.id.jc_betcode);
        if (togetherbuyOrderDetail.getWincode() != null && togetherbuyOrderDetail.getWincode().toString().contains(",")) {
            winCodes = togetherbuyOrderDetail.getWincode().toString().split(",");
        }
        List<ZCOrderInfo.DetailBean.RaceBetcodeBean> raceBetcodeBeens = info.getDetail()
                .getRaceBetcode();
        ZCBetCodeAdapter betCodeAdapter = new ZCBetCodeAdapter(raceBetcodeBeens);
        ls.setAdapter(betCodeAdapter);
        return view;
    }


    protected View addJCContent(JCOrderInfo info) {
        View view = View.inflate(this, R.layout.layout_jcbetcode, null);
        TextView playName = (TextView) view.findViewById(R.id.playName);
        playName.setText("玩法:" + info.getDetail().getPlayName());
        ListView ls = (ListView) view.findViewById(R.id.jc_betcode);
        List<JCOrderInfo.DetailBean.RaceBetcodeBean> raceBetcodeBeens = info.getDetail()
                .getRaceBetcode();
        BetCodeAdapter betCodeAdapter = new BetCodeAdapter(raceBetcodeBeens, info);
        ls.setAdapter(betCodeAdapter);
        return view;
    }

    class BetCodeAdapter extends BaseAdapter {
        private List<JCOrderInfo.DetailBean.RaceBetcodeBean> raceBetcodeBeens;
        private JCOrderInfo info;

        BetCodeAdapter(List<JCOrderInfo.DetailBean.RaceBetcodeBean> raceBetcodeBeens, JCOrderInfo
                info) {
            this.raceBetcodeBeens = raceBetcodeBeens;
            this.info = info;
        }

        @Override
        public int getCount() {
            return raceBetcodeBeens.size();
        }

        @Override
        public JCOrderInfo.DetailBean.RaceBetcodeBean getItem(int position) {
            return raceBetcodeBeens.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            BetCodeAdapter.ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(HeMaiGenDanActivity.this, R.layout
                        .layout_jcbetcode_item, null);
                holder = new BetCodeAdapter.ViewHolder();
                holder.teamname = convertView.findViewById(R.id.team_homeaway);
                holder.result = convertView.findViewById(R.id.list_contentsbean);
                holder.id = convertView.findViewById(R.id.team_id);
                holder.isDan = convertView.findViewById(R.id.team_isDan);
                holder.score = convertView.findViewById(R.id.team_score);
                convertView.setTag(holder);
            } else {
                holder = (BetCodeAdapter.ViewHolder) convertView.getTag();
            }
            JCOrderInfo.DetailBean.RaceBetcodeBean raceBetcodeBean = raceBetcodeBeens.get(position);
            if (raceBetcodeBean.getDan() == 1) {
                holder.isDan.setVisibility(View.VISIBLE);
            } else {
                holder.isDan.setVisibility(View.INVISIBLE);
            }
            String week = raceBetcodeBean.getOfficialWeekDay();
            String id = "";
            if (TextUtil.isEmpty(week)) {
                id = raceBetcodeBean.getMatchNum();
            } else {
                id = week + "\n" + raceBetcodeBean.getMatchNum();
            }
            holder.id.setText(id);
            String team;
            if (info.getLotteryType() == LotteryIDUtils.JLDXF_LOTTERY_ID_INT || info
                    .getLotteryType() == LotteryIDUtils.JLHH_LOTTERY_ID_INT || info
                    .getLotteryType() == LotteryIDUtils.JLSF_LOTTERY_ID_INT || info
                    .getLotteryType() == LotteryIDUtils.JLRSF_LOTTERY_ID_INT || info
                    .getLotteryType() == LotteryIDUtils.JLSFC_LOTTERY_ID_INT) {
                team = raceBetcodeBean
                        .getAwayTeam() + "(客)\nVS\n" + raceBetcodeBean.getHomeTeam() + "(主)";
            } else {
                team = raceBetcodeBean.getHomeTeam() + "(主)\n" + raceBetcodeBean.getHandicap() + "\n" + raceBetcodeBean
                        .getAwayTeam() + "(客)";
            }
            holder.teamname.setText(team);
            String homeScore = "-";
            String awawScore = "-";
            if (!TextUtil.isEmpty(raceBetcodeBean.getHomeScore())) {
                homeScore = raceBetcodeBean.getHomeScore();
            }
            if (!TextUtil.isEmpty(raceBetcodeBean.getAwayScore())) {
                awawScore = raceBetcodeBean.getAwayScore();
            }
            holder.score.setText(homeScore + " : " + awawScore);

            CodeContentAdapter codeContentAdapter = new
                    CodeContentAdapter(raceBetcodeBean
                    .getContents());
            holder.result.setAdapter(codeContentAdapter);
            return convertView;
        }

        class ViewHolder {
            TextView isDan;
            TextView id;
            NoScrollListView result;
            TextView teamname;
            TextView score;
        }
    }

    class CodeContentAdapter extends BaseAdapter {
        private List<JCOrderInfo.DetailBean.RaceBetcodeBean.ContentsBean> raceBetcodeBeens;

        CodeContentAdapter(List<JCOrderInfo.DetailBean.RaceBetcodeBean.ContentsBean>
                                   raceBetcodeBeens) {
            this.raceBetcodeBeens = raceBetcodeBeens;
        }

        @Override
        public int getCount() {
            return raceBetcodeBeens.size();
        }

        @Override
        public JCOrderInfo.DetailBean.RaceBetcodeBean.ContentsBean getItem(int position) {
            return raceBetcodeBeens.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            CodeContentAdapter.ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(HeMaiGenDanActivity.this, R.layout
                        .layout_jcbetcode_contentbean_item, null);
                holder = new CodeContentAdapter.ViewHolder();
                holder.payType = convertView.findViewById(R.id.team_paytype);
                holder.result = convertView.findViewById(R.id.team_result);
                holder.betcode = convertView.findViewById(R.id.team_betcontent);
                holder.line = convertView.findViewById(R.id.team_result_line);
                convertView.setTag(holder);
            } else {
                holder = (CodeContentAdapter.ViewHolder) convertView.getTag();
            }
            JCOrderInfo.DetailBean.RaceBetcodeBean.ContentsBean raceBetcodeBean =
                    raceBetcodeBeens.get(position);
            holder.payType.setText(raceBetcodeBean.getLotteryTypeName());
            String resultStr = raceBetcodeBean.getResult();
            if (TextUtil.isEmpty(resultStr)) {
                resultStr = "--";
            }
            holder.result.setText(Html.fromHtml(resultStr));
            holder.betcode.setText(Html.fromHtml(raceBetcodeBean.getBetcode().replace(",","\n")));

            if (position == raceBetcodeBeens.size()-1) {
                holder.line.setVisibility(View.GONE);
            } else {
                holder.line.setVisibility(View.VISIBLE);
            }
            return convertView;
        }

        class ViewHolder {
            TextView payType;
            TextView result;
            TextView betcode;
            View line;
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        DJ_ProgressDialog.show(getContext(), true, true);
        if (MySharedPreferences.getInstance().getLoginStatus()) {
            userName = MySharedPreferences.getInstance().getUserName();
        }
    }

    private void initView() {
        String all_moneystr = "共<font color=\"#f8dd26\">" + buyAmount + "</font>米";
        amount_pay_txt.setText(Html.fromHtml(all_moneystr));
        topView.setCenterText(R.string.hm_xiangqing);
        topView.setRightVisible(View.VISIBLE);
        topView.setOnTitleClickListener(this);
        fg_persons.setOnClickListener(this);
        fg_ordercontent.setOnClickListener(this);
        fg_msg.setOnClickListener(this);

        persons_more_tv.setOnClickListener(this);
    }

    /**
     * 设置认购输入框最大值
     *
     * @param leftMoney
     */
    private void renGouSet(String leftMoney) {
        double leftM = 0;
        try {
            leftM = Double.parseDouble(leftMoney);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (leftM == 0) {
        } else {
        }
    }


    //====================	top	===================
    @Override
    public void onLeftClick() {
        // TODO Auto-generated method stub
        onBackPressed();
    }

    @Override
    public void onCenterClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRightClick() {
        requestHMDetail();
    }

    @Override
    public void onRight2Click() {
        // TODO Auto-generated method stub

    }

    private int getBaiFenBi(int buyMoney, int totalAmount) {
        if (buyMoney == 0 || totalAmount == 0) {
            return 0;
        }
        int result = (int) (buyMoney * 1.0 / totalAmount * 100);
        return result == 0 ? 1 : result;
    }

    private int getBaiFenBi(int buyMoney, String totalAmount) {
        int totalAmount1 = 0;
        try {
            totalAmount1 = (int) Double.parseDouble(totalAmount);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return getBaiFenBi(buyMoney, totalAmount1);
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.fg_persons:
                if (persons_ll.getVisibility() == View.GONE) {
                    persons_ll.setVisibility(View.VISIBLE);
                    orderpersons_jietou.setImageResource(R.drawable.top_jietou);
                } else {
                    persons_ll.setVisibility(View.GONE);
                    orderpersons_jietou.setImageResource(R.drawable.right_jietou_black);
                }
                break;
            case R.id.fg_content_msg:
                if (ll_content_msg.getVisibility() == View.GONE) {
                    ll_content_msg.setVisibility(View.VISIBLE);
                    ordermsg_jietou.setImageResource(R.drawable.top_jietou);
                } else {
                    ll_content_msg.setVisibility(View.GONE);
                    ordermsg_jietou.setImageResource(R.drawable.right_jietou_black);
                }
                break;
            case R.id.fg_ordercontent:
                if (fg_gendan_mingxi.getVisibility() == View.GONE) {
                    fg_gendan_mingxi.setVisibility(View.VISIBLE);
                    ordercontent_jietou.setImageResource(R.drawable.top_jietou);
                } else {
                    fg_gendan_mingxi.setVisibility(View.GONE);
                    ordercontent_jietou.setImageResource(R.drawable.right_jietou_black);
                }
                break;
            case R.id.persons_more_tv:
                persons.clear();
                persons.addAll(personsInfors);
                personsAdapter.notifyDataSetChanged();
                persons_more_tv.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    String[] winCodes;

    class ZCBetCodeAdapter extends BaseAdapter {

        private List<ZCOrderInfo.DetailBean.RaceBetcodeBean> raceBetcodeBeens;

        ZCBetCodeAdapter(List<ZCOrderInfo.DetailBean.RaceBetcodeBean> raceBetcodeBeens) {
            this.raceBetcodeBeens = raceBetcodeBeens;
        }

        @Override
        public int getCount() {
            return raceBetcodeBeens.size();
        }

        @Override
        public ZCOrderInfo.DetailBean.RaceBetcodeBean getItem(int position) {
            return raceBetcodeBeens.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(HeMaiGenDanActivity.this, R.layout
                        .layout_zcbetcode_item, null);
                holder = new ViewHolder();
                holder.teamname = (TextView) convertView.findViewById(R.id.team);
                holder.betcode = (TextView) convertView.findViewById(R.id.betcode);
                holder.id = (TextView) convertView.findViewById(R.id.team_id);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            ZCOrderInfo.DetailBean.RaceBetcodeBean raceBetcodeBean = raceBetcodeBeens.get(position);

            int id = raceBetcodeBean.getMatchNum();
            holder.id.setText(id + "");
            String team = raceBetcodeBean.getHomeTeam() + " VS " + raceBetcodeBean
                    .getAwayTeam();
            holder.teamname.setText(team);
            if (getLotteryId().equals(LotteryIDUtils.ZCJQC_LOTTERY_ID_INT + "")) {
                holder.betcode.setText("主:" + raceBetcodeBean.getHbetcode() + "\n" + "客:" + raceBetcodeBean.getAbetcode());
            } else if (getLotteryId().equals(LotteryIDUtils.ZCBQC_LOTTERY_ID_INT + "")) {
                holder.betcode.setText("半:" + raceBetcodeBean.getBbetcode() + "\n" + "全:" + raceBetcodeBean.getQbetcode());
            } else {
                if (winCodes != null && raceBetcodeBean.getBetcode().equals(winCodes[id - 1])) {
                    holder.betcode.setTextColor(Color.RED);
                } else {
                    holder.betcode.setTextColor(getResources().getColor(R.color.color_333333));
                }
                holder.betcode.setText(raceBetcodeBean.getBetcode());
            }

            return convertView;
        }


        class ViewHolder {
            TextView id;
            TextView teamname;
            TextView betcode;

        }
    }

    private String getLotteryId() {
        return togetherbuyOrderDetail.getLotteryType() + "";
    }


}
